﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using DungChung;
using System.IO;
namespace CoVuaClient
{
    /// <summary>
    /// Interaction logic for DangNhapWindow.xaml
    /// </summary>
    public partial class DangNhapWindow : Window
    {
        public DangNhapWindow()
        {
            InitializeComponent();
            ipServer = "";
        }
        private TcpClient _tcpClient;

        public string ipServer;
        public TcpClient tcpClient
        {
            get { return _tcpClient; }
            set { _tcpClient = value; }
        }


        private void buttonDangNhap_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(IPAddress.Parse(ipServer), 8000);
                NetworkStream stream = tcpClient.GetStream();
                BinaryReader reader = new BinaryReader(stream);
                BinaryWriter writer = new BinaryWriter(stream);
                writer.Write(MyTask.DangNhap);
                writer.Write(textBoxUsername.Text);
                writer.Write(textBoxPassword.Password);
                string kq = reader.ReadString();
                if (kq == MyTask.DangNhapThanhCong)
                {
                    this.Hide();
                }
                else
                {
                    MessageBox.Show(kq);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            } 
        }

        private void buttonThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void textBlockDangKi_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DangKi dangKi = new DangKi();
            dangKi.ipServer = ipServer;
            dangKi.Owner = this;
            this.Hide();
            dangKi.ShowDialog();
            this.ShowDialog();
        }
    }
}
